package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Medicine;
import com.itheima.pojo.Travel;
import com.itheima.service.MedicineService;
import com.itheima.service.TravelService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Reference
    private MedicineService medicineService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return medicineService.findPage(queryPageBean);
    }


    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        try {
            List<String[]> list = POIUtils.readExcel(file);
            if (list != null && list.size() > 0) {
                ArrayList<Medicine> medicines = new ArrayList<>();
                for (String[] strings : list) {
                    String name = strings[0];
                    String type = strings[1];
                    String remark = strings[2];
                    String number = strings[3];
                    String size = strings[4];

                    Medicine medicine = new Medicine(name,type,remark,number,size);
                    medicines.add(medicine);
                }
                medicineService.add(medicines);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
}
